index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. const WXAPI = require('apifm-wxapi')
  2. const AUTH = require('../../../utils/auth')
  3. const ImageUtil = require('../../../utils/image')
  4. const APP = getApp()
  5. var sliderWidth = 96; // 需要设置slider的宽度,用于计算中间位置
  6. Date.prototype.format = function(format) {
  7. var date = {
  8. "M+": this.getMonth() + 1,
  9. "d+": this.getDate(),
  10. "h+": this.getHours(),
  11. "m+": this.getMinutes(),
  12. "s+": this.getSeconds(),
  13. "q+": Math.floor((this.getMonth() + 3) / 3),
  14. "S+": this.getMilliseconds()
  15. };
  16. if (/(y+)/i.test(format)) {
  17. format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  18. }
  19. for (var k in date) {
  20. if (new RegExp("(" + k + ")").test(format)) {
  21. format = format.replace(RegExp.$1, RegExp.$1.length == 1
  22. ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
  23. }
  24. }
  25. return format;
  26. }
  27. Page({
  28. /**
  29. * 页面的初始数据
  30. */
  31. data: {
  32. balance: 0,
  33. freeze: 0,
  34. fxCommisionPaying: 0,
  35. score: 0,
  36. commisionData: {
  37. today: 0,
  38. yesday: 0,
  39. thisMonth: 0,
  40. lastMonth: 0,
  41. todayXiaoshou: 0,
  42. yesdayXiaoshou: 0,
  43. thisMonthXiaoshou: 0,
  44. lastMonthXiaoshou: 0,
  45. },
  46. },
  47. /**
  48. * 生命周期函数--监听页面加载
  49. */
  50. onLoad: function (options) {
  51. // wx.setStorageSync('token', '4f02de6e-914f-4439-a128-a62a6bbdc3e4')
  52. this.adPosition()
  53. },
  54. onShow: function () {
  55. AUTH.checkHasLogined().then(isLogined => {
  56. if (isLogined) {
  57. this.doneShow();
  58. this.doneShow2();
  59. this.getUserApiInfo();
  60. }
  61. })
  62. },
  63. async commision() {
  64. const uid = this.data.apiUserInfoMap.base.id
  65. var commisionData = this.data.commisionData
  66. const nowDate = new Date()
  67. console.log('今天', nowDate.format('yyyyMMdd'))
  68. console.log('本月', nowDate.format('yyyyMM'))
  69. const yestoday = new Date(nowDate.getTime() - 24 * 60 * 60 * 1000)
  70. console.log('昨天', yestoday.format('yyyyMMdd'))
  71. // 上个月
  72. let year = nowDate.getFullYear()
  73. let month = nowDate.getMonth() + 1
  74. if (month == 1) {
  75. month = 12
  76. year--
  77. } else {
  78. month--
  79. }
  80. const lastMonth = year + "" + (month < 10 ? ('0' + month) : month)
  81. console.log('上个月', lastMonth)
  82. let res = await WXAPI.siteStatisticsSaleroom({
  83. dateBegin: nowDate.format('yyyyMMdd'),
  84. dateEnd: nowDate.format('yyyyMMdd'),
  85. uid: uid
  86. })
  87. if (res.code === 0) {
  88. commisionData.today = res.data[0].estimateCommission
  89. commisionData.todayXiaoshou = res.data[0].saleroom
  90. }
  91. res = await WXAPI.siteStatisticsSaleroom({
  92. dateBegin: yestoday.format('yyyyMMdd'),
  93. dateEnd: yestoday.format('yyyyMMdd'),
  94. uid: uid
  95. })
  96. if (res.code === 0) {
  97. commisionData.yesday = res.data[0].estimateCommission
  98. commisionData.yesdayXiaoshou = res.data[0].saleroom
  99. }
  100. res = await WXAPI.siteStatisticsSaleroom({
  101. dateBegin: nowDate.format('yyyyMM'),
  102. dateEnd: nowDate.format('yyyyMM'),
  103. uid: uid
  104. })
  105. if (res.code === 0) {
  106. commisionData.thisMonth = res.data[0].estimateCommission
  107. commisionData.thisMonthXiaoshou = res.data[0].saleroom
  108. }
  109. res = await WXAPI.siteStatisticsSaleroom({
  110. dateBegin: lastMonth,
  111. dateEnd: lastMonth,
  112. uid: uid
  113. })
  114. if (res.code === 0) {
  115. commisionData.lastMonth = res.data[0].estimateCommission
  116. commisionData.lastMonthXiaoshou = res.data[0].saleroom
  117. }
  118. this.setData({
  119. commisionData:commisionData
  120. })
  121. },
  122. getUserApiInfo: function () {
  123. var that = this;
  124. WXAPI.userDetail(wx.getStorageSync('token')).then(function (res) {
  125. if (res.code == 0) {
  126. let _data = {}
  127. _data.apiUserInfoMap = res.data
  128. that.setData(_data);
  129. that.commision();
  130. if (res.data.base.isSeller) {
  131. // 判断是否是市区合伙人
  132. that.fxCities()
  133. }
  134. }
  135. })
  136. },
  137. doneShow: function () {
  138. const _this = this
  139. const token = wx.getStorageSync('token')
  140. if (!token) {
  141. return
  142. }
  143. WXAPI.userAmount(token).then(function (res) {
  144. if (res.code == 700) {
  145. wx.showToast({
  146. title: '当前账户存在异常',
  147. icon: 'none'
  148. })
  149. return
  150. }
  151. if (res.code == 2000) {
  152. return
  153. }
  154. if (res.code == 0) {
  155. _this.setData({
  156. balance: res.data.balance.toFixed(2),
  157. freeze: res.data.freeze.toFixed(2),
  158. fxCommisionPaying: res.data.fxCommisionPaying.toFixed(2),
  159. totleConsumed: res.data.totleConsumed.toFixed(2),
  160. score: res.data.score
  161. });
  162. }
  163. })
  164. },
  165. copyContent(e) {
  166. var data = e.currentTarget.dataset.id + ""
  167. wx.setClipboardData({
  168. data: data
  169. })
  170. },
  171. async doneShow2() {
  172. const _this = this
  173. const userDetail = await WXAPI.userDetail(wx.getStorageSync('token'))
  174. WXAPI.fxApplyProgress(wx.getStorageSync('token')).then(res => {
  175. let applyStatus = userDetail.data.base.isSeller ? 2 : -1
  176. if (res.code == 2000) {
  177. return
  178. }
  179. if (res.code === 700) {
  180. _this.setData({
  181. applyStatus: applyStatus
  182. })
  183. }
  184. if (res.code === 0) {
  185. if (userDetail.data.base.isSeller) {
  186. applyStatus = 2
  187. } else {
  188. applyStatus = res.data.status
  189. }
  190. _this.setData({
  191. applyStatus: applyStatus,
  192. applyInfo: res.data
  193. })
  194. }
  195. if (applyStatus == 2) {
  196. _this.fetchQrcode()
  197. }
  198. })
  199. },
  200. fetchQrcode(){
  201. const _this = this
  202. wx.showLoading({
  203. title: '加载中',
  204. mask: true
  205. })
  206. WXAPI.wxaQrcode({
  207. scene: 'inviter_id=' + wx.getStorageSync('uid'),
  208. page: 'pages/index/index',
  209. is_hyaline: true,
  210. autoColor: true,
  211. expireHours: 1
  212. }).then(res => {
  213. wx.hideLoading()
  214. if (res.code == 41030) {
  215. wx.showToast({
  216. title: '上线以后才可以获取二维码',
  217. icon: 'none'
  218. })
  219. return
  220. }
  221. if (res.code == 0) {
  222. _this.showCanvas(res.data)
  223. }
  224. })
  225. },
  226. showCanvas(qrcode){
  227. const _this = this
  228. let ctx
  229. wx.getImageInfo({
  230. src: qrcode,
  231. success: (res) => {
  232. const imageSize = ImageUtil.imageUtil(res.width, res.height)
  233. const qrcodeWidth = imageSize.windowWidth / 2
  234. _this.setData({
  235. canvasHeight: qrcodeWidth
  236. })
  237. ctx = wx.createCanvasContext('firstCanvas')
  238. ctx.setFillStyle('#FDF3E7')
  239. ctx.fillRect(0, 0, imageSize.windowWidth, imageSize.imageHeight + qrcodeWidth)
  240. ctx.drawImage(res.path, 0, 0, qrcodeWidth, qrcodeWidth)
  241. setTimeout(function () {
  242. wx.hideLoading()
  243. ctx.draw()
  244. }, 1000)
  245. }
  246. })
  247. },
  248. saveToMobile() {
  249. wx.canvasToTempFilePath({
  250. canvasId: 'firstCanvas',
  251. success: function (res) {
  252. let tempFilePath = res.tempFilePath
  253. wx.saveImageToPhotosAlbum({
  254. filePath: tempFilePath,
  255. success: (res) => {
  256. wx.showModal({
  257. content: '二维码已保存到手机相册',
  258. showCancel: false,
  259. confirmText: '知道了',
  260. confirmColor: '#333'
  261. })
  262. },
  263. fail: (res) => {
  264. wx.showToast({
  265. title: res.errMsg,
  266. icon: 'none'
  267. })
  268. }
  269. })
  270. }
  271. })
  272. },
  273. // 读取市区合伙人
  274. async fxCities() {
  275. const res = await WXAPI.fxCities(wx.getStorageSync('token'))
  276. if (res.code == 0) {
  277. this.setData({
  278. fxCities: res.data
  279. })
  280. }
  281. },
  282. // 读取广告位
  283. async adPosition() {
  284. const res = await WXAPI.adPosition('fx_index')
  285. if (res.code == 0) {
  286. this.setData({
  287. fxIndexAdPos: res.data
  288. })
  289. }
  290. },
  291. goUrl(e) {
  292. const url = e.currentTarget.dataset.url
  293. if (url) {
  294. wx.navigateTo({
  295. url: url,
  296. })
  297. }
  298. },
  299. onShareAppMessage() {
  300. return {
  301. title: '"' + wx.getStorageSync('mallName') + '" ' + wx.getStorageSync('share_profile'),
  302. path: '/pages/index/index?inviter_id=' + wx.getStorageSync('uid'),
  303. imageUrl: wx.getStorageSync('share_pic'),
  304. success: function (res) {
  305. // 转发成功
  306. },
  307. fail: function (res) {
  308. // 转发失败
  309. }
  310. }
  311. },
  312. goApply() {
  313. wx.redirectTo({
  314. url: '/packageFx/pages/apply/index',
  315. })
  316. }
  317. })