awaitState.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // pages/awaitState/awaitState.js
  2. var app = getApp();
  3. var Util = require('../../utils/util.js');
  4. Page({
  5. data: {
  6. src: '',
  7. msg: '',
  8. url: '',
  9. name: '',
  10. order_number: '',
  11. order_userid: '',
  12. sendUserId: '',
  13. recHidden: false,
  14. sendHidden: false,
  15. time: '',
  16. uploadImg: [],
  17. newImgList: [],
  18. order_type: '0',
  19. goodsList: [],
  20. userInfo: {},
  21. showSendFriendMsg: false,//送朋友弹窗
  22. goodsId: 0, //送朋友-当前商品在列表中的id
  23. linkFlag: -1,
  24. shareTime: '',
  25. footerHidden: true, //默认底部按钮隐藏
  26. authorize: 0, //授权标志位
  27. userInfo: {},//用户信息
  28. order_number_send: '',//拆分的订单号
  29. hiddenmodal: true, //提示弹窗 - 初始隐藏
  30. modalCont: '',//提示弹窗内容
  31. addGiftListClick: false, //判断“放入礼物盒”按钮是否点击过
  32. wrapShowFlag : '', //显示领取弹窗
  33. loadingFinish : false, //全局加载完毕
  34. fromUserRole : '', //分享者-用户角色
  35. },
  36. onLoad: function (options) {
  37. var that = this;
  38. var host = getApp().globalData.servsers;
  39. var un_id = getApp().globalData.un_id;
  40. var order_number = options.order_number;
  41. var order_number_send = options.order_number_send;
  42. var shareTime = options.shareTime;
  43. var goodsList = options.goodsList;
  44. goodsList = goodsList.replace(/zss/g, "&");
  45. goodsList = JSON.parse(goodsList);
  46. console.log(goodsList);
  47. that.setData({
  48. src: options.src,
  49. msg: options.msg,
  50. url: options.url,
  51. name: options.name,
  52. order_number: options.order_number,//拆分的订单号
  53. order_userid: options.order_userid,//送礼物者的union_id
  54. time: options.time,
  55. uploadImg: options.uploadImg,
  56. newImgList: (options.uploadImg).split(","),
  57. order_type: '0',
  58. goodsName: options.goodsName,
  59. goodsNum: options.goodsNum,
  60. goodsList: goodsList,
  61. shareTime: options.shareTime,
  62. order_number_send: options.order_number_send,//赠送的订单号
  63. addGiftListClick: false, //判断“放入礼物盒”按钮是否点击过
  64. wrapShowFlag: '',
  65. loadingFinish:false,
  66. fromUserRole: options.fromUserRole,
  67. userInfo: getApp().globalData.userInfo
  68. })
  69. console.log("awaitState-userRole:" + options.fromUserRole);
  70. },
  71. //放入礼物盒
  72. addGiftList: function (e) {
  73. var that = this;
  74. var host = getApp().globalData.servsers;
  75. var un_id = getApp().globalData.un_id;
  76. var openid = getApp().globalData.openid;
  77. var isNewUser = getApp().globalData.isNewUser;//是否是新用户
  78. var order_number = that.data.order_number_send;
  79. var nickName = that.data.userInfo.nickName;
  80. console.log("openid:" + openid);
  81. var time = Util.formatTime(new Date()); //获赠时间
  82. if ((un_id == undefined || un_id == '' || un_id == null) || (openid == undefined || openid == '' || openid == null) || (nickName == undefined || nickName == '' || nickName == null)) {
  83. wx.navigateTo({
  84. url: '/pages/authorize/authorize?link=awaitState',
  85. })
  86. } else {
  87. var timestamp = Date.parse(new Date());
  88. var formIdTime = timestamp / 1000;//formId 生成时间
  89. var formId = e.detail;//formId
  90. console.log(formId);
  91. if (isNewUser) {//新用户 - 创建分销关系
  92. that.createRelation();
  93. }
  94. //针对消息模版:存储模板消息使用的form_id【操作人产生的form_id,仅可用于给当前操作人发送消息】
  95. wx.request({
  96. url: host + "userapi/insertadduserwxsend",
  97. data: {
  98. user_id: un_id,
  99. form_id_dz: formId.formId,
  100. order_number: order_number,//6开头的单号
  101. },
  102. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  103. header: {
  104. 'Accept': 'application/json'
  105. },
  106. success: function (res) {
  107. if (res == null || res.data == null) {
  108. console.error('网络请求失败');
  109. return;
  110. }
  111. }
  112. })
  113. that.setData({
  114. addGiftListClick: true
  115. })
  116. //查询同一分享链接,礼物接收状态【根据订单号和时间做唯一标识,0为未被接收,1为被接收】
  117. wx.request({
  118. url: host + "orderapi/findlinkByOrderNumber",
  119. data: {
  120. order_number: that.data.order_number,
  121. order_timeno: that.data.shareTime
  122. },
  123. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  124. header: {
  125. 'Accept': 'application/json'
  126. },
  127. success: function (res) {
  128. console.log(res.data);
  129. if (res.data == 0) {
  130. that.addGiftListFun(un_id, time);
  131. wx.showLoading({
  132. title: '领取中,请稍候',
  133. icon: 'loading',
  134. mask: true,
  135. success: function () {
  136. }
  137. })
  138. } else {
  139. that.setData({
  140. hiddenmodal: false,
  141. modalCont: '礼物已被抢走了'
  142. })
  143. setTimeout(function () {
  144. that.setData({
  145. hiddenmodal: true
  146. })
  147. }, 1000)
  148. return false;
  149. }
  150. that.setData({
  151. linkFlag: res.data,
  152. footerHidden: false
  153. })
  154. console.log(res.data);
  155. }
  156. })
  157. }
  158. },
  159. //获取用户角色
  160. getUserRole: function (id) {
  161. var that = this;
  162. var host = getApp().globalData.servsers;
  163. wx.login({
  164. success: function (res) {
  165. var code = res.code;
  166. wx.getUserInfo({
  167. withCredentials: true,
  168. success: function (res) {
  169. console.log("aaa:");
  170. console.log(res);
  171. getApp().globalData.userInfo = res.userInfo;
  172. wx.request({
  173. url: host + "api/distributeSell/getUserRole",
  174. data: {
  175. code: code,
  176. iv: res.iv,
  177. encryptedData: res.encryptedData,
  178. },
  179. method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  180. header: {
  181. 'content-type': 'application/x-www-form-urlencoded'
  182. },
  183. success: function (res) {
  184. console.log("获取用户角色:");
  185. console.log(res);
  186. //成功
  187. if (res.data.code == '200') {
  188. getApp().globalData.un_id = res.data.data.user_id;
  189. getApp().globalData.openid = res.data.data.openid;
  190. getApp().globalData.isNewUser = false;//不是新用户
  191. getApp().globalData.userRole = res.data.data.userRole;
  192. getApp().globalData.userInfo.avatarUrl = res.data.data.icon;
  193. getApp().globalData.userInfo.nickName = res.data.data.name;
  194. console.log("getUserRole-openid:" + res.data.data.openid);
  195. console.log("getUserRole-isNewUser:" + getApp().globalData.isNewUser);
  196. console.log("getUserRole-userRole:" + res.data.data.userRole);
  197. if (that.employIdCallback) {
  198. that.employIdCallback(res.data.data.user_id);
  199. }
  200. if (that.employIdCallback2) {
  201. that.employIdCallback2(res.data.data.openid);
  202. }
  203. if (that.employIdCallbackUser) {
  204. that.employIdCallbackUser(false);
  205. }
  206. if (that.employIdCallbackRole) {
  207. that.employIdCallbackRole(res.data.data.userRole);
  208. }
  209. } else {
  210. wx.navigateTo({
  211. url: '/pages/authorize/authorize?link=midAutumn',
  212. })
  213. }
  214. if (res.data.data == '') {
  215. that.globalData.isNewUser = true;//是新用户
  216. console.log("getUserRole-isNewUser:" + res.data.isNewUser);
  217. }
  218. if (res == null || res.data == null) {
  219. console.error('网络请求失败');
  220. return;
  221. }
  222. }
  223. });
  224. },
  225. fail: function (e) {
  226. console.log("需重新授权");
  227. }
  228. })
  229. }
  230. });
  231. },
  232. //对应更改礼物流程方法
  233. addGiftListFun: function (un_id, time) {
  234. var that = this;
  235. var host = getApp().globalData.servsers;
  236. //更改当前链接下,接收礼物的状态为1
  237. // wx.request({
  238. // url: host + "orderapi/updateOrder_link",
  239. // data: {
  240. // order_number: that.data.order_number,
  241. // order_timeno: that.data.shareTime,
  242. // order_linkFlag: 1
  243. // },
  244. // method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  245. // header: {
  246. // 'Accept': 'application/json'
  247. // },
  248. // success: function (res) {
  249. console.log("from_user_id:" + that.data.order_userid);
  250. console.log("un_id:" + un_id);
  251. console.log("user_head:" + that.data.userInfo.avatarUrl);
  252. console.log("user_name:" + that.data.userInfo.nickName);
  253. console.log("time:" + time);
  254. //对应用户保存收到的礼物
  255. wx.request({
  256. url: host + "giftapi/insertUserGift",
  257. data: {
  258. userId: un_id,
  259. giftImg: that.data.src,
  260. giftMsg: that.data.msg,
  261. fromUserHead: that.data.url,
  262. fromUserName: that.data.name,
  263. orderNumber: that.data.order_number,
  264. orderNumberGift: that.data.order_number_send,//拆分的订单号
  265. cTime: time,//获赠时间
  266. giftOtherImg: that.data.uploadImg,
  267. user_head: that.data.userInfo.avatarUrl,//收礼物者头像
  268. user_name: that.data.userInfo.nickName,//收礼物者微信名
  269. from_user_id: that.data.order_userid,//送礼物者union_id
  270. receive_flag: 1 //礼物接收状态 : 已接收
  271. },
  272. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  273. header: {
  274. 'Accept': 'application/json'
  275. },
  276. success: function (res) {
  277. console.log(res.data.flag);
  278. //已经被领取
  279. if (res.data.flag == false){
  280. wx.hideLoading();
  281. that.setData({
  282. hiddenmodal: false,
  283. modalCont: '礼物已被抢走了'
  284. })
  285. setTimeout(function () {
  286. that.setData({
  287. hiddenmodal: true,
  288. linkFlag : 1,
  289. wrapShowFlag : ''
  290. })
  291. }, 1000)
  292. return false;
  293. } else if (res.data.flag == true){
  294. console.log("接收礼物成功!");
  295. //更改"送礼物表",回显领取状态的标志位 - receive_flag 【 0 : 未接收 1:已接收 2:超时未领取 】
  296. wx.request({
  297. url: host + "giftapi/updateReceive_flag_send",
  298. data: {
  299. order_number: that.data.order_number,
  300. order_number_gift: that.data.order_number_send,
  301. },
  302. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  303. header: {
  304. 'Accept': 'application/json'
  305. },
  306. success: function (res) {
  307. var preFlag = that.data.flag;
  308. wx.hideLoading();
  309. that.setData({
  310. hiddenmodal: false,
  311. modalCont: '礼物领取成功'
  312. })
  313. setTimeout(function () {
  314. that.setData({
  315. hiddenmodal: true
  316. })
  317. wx.redirectTo({
  318. url: '/pages/mineGiftList/mineGiftList?isSelectNew=1',
  319. })
  320. }, 1000)
  321. if (res == null || res.data == null) {
  322. that.setData({
  323. addGiftListClick: false
  324. })
  325. console.error('网络请求失败');
  326. return;
  327. }
  328. }
  329. });
  330. //更改 order表 ,当前链接下,接收礼物的状态为1
  331. wx.request({
  332. url: host + "orderapi/updateOrder_link",
  333. data: {
  334. order_number: that.data.order_number,
  335. order_timeno: that.data.shareTime,
  336. order_linkFlag: 1
  337. },
  338. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  339. header: {
  340. 'Accept': 'application/json'
  341. },
  342. success: function (res) {
  343. }
  344. })
  345. //修改送礼物 - 接收状态为1【已接收】 【订单列表】
  346. wx.request({
  347. url: host + "orderapi/updateOrder_linkFlag",
  348. data: {
  349. order_number: that.data.order_number,
  350. linkFlag: 1
  351. },
  352. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  353. header: {
  354. 'Accept': 'application/json'
  355. },
  356. success: function (res) {
  357. if (res == null || res.data == null) {
  358. console.error('网络请求失败');
  359. return;
  360. }
  361. }
  362. });
  363. }else{
  364. wx.hideLoading();
  365. }
  366. if (res == null || res.data == null) {
  367. console.error('网络请求失败');
  368. return;
  369. }
  370. }
  371. });
  372. // }
  373. // })
  374. },
  375. //创建分销关系-当被分享用户通过分享进入客户端时进行关系确立
  376. createRelation: function (e) {
  377. var that = this;
  378. var host = getApp().globalData.servsers;
  379. var un_id = getApp().globalData.un_id;
  380. var fromUserId = that.data.order_userid;
  381. var fromUserRole = that.data.fromUserRole;
  382. console.log("awaitState-fromUserId:" + fromUserId);
  383. console.log("awaitState-un_id:" + un_id);
  384. console.log("awaitState-userRole:" + fromUserRole);
  385. wx.request({
  386. url: host + "api/distributeSell/createRelation",
  387. data: {
  388. shareUserId: fromUserId,//分享者 - 用户userId
  389. currentUserId: un_id,//当前操作用户userId
  390. userRole: fromUserRole,//分享者 - 用户角色 0:普通用户 1:商务用户
  391. },
  392. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  393. header: {
  394. 'Accept': 'application/json'
  395. },
  396. success: function (res) {
  397. if (res.data.code == '200') {
  398. console.log('分销关系建立成功');
  399. }
  400. if (res == null) {
  401. console.log('网络异常,请重新操作!');
  402. return;
  403. }
  404. }
  405. })
  406. },
  407. onShow: function () {
  408. var that = this;
  409. var authorize = that.data.authorize;
  410. wx.showLoading({
  411. title: '加载中,请稍候',
  412. icon: 'loading',
  413. mask: true,
  414. success: function () {
  415. }
  416. })
  417. wx.getSetting({
  418. success: function (res) {
  419. console.log(res);
  420. if (res.authSetting['scope.userInfo']) {//授权过
  421. //对于授权过小程序的用户,获取用户信息
  422. that.getUserRole();
  423. }
  424. }
  425. })
  426. var host = getApp().globalData.servsers;
  427. var un_id = getApp().globalData.un_id;
  428. //查询订单状态 - 订单表
  429. wx.request({
  430. url: host + "orderapi/findOrderTypeByOrderNumber",
  431. data: {
  432. order_number: that.data.order_number//拆分的订单号
  433. },
  434. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  435. header: {
  436. 'Accept': 'application/json'
  437. },
  438. success: function (res) {
  439. console.log("状态:" + res.data);
  440. that.setData({
  441. order_type: res.data
  442. })
  443. if (res.data != 2) {//朋友未完全收下礼物【可能转送了】
  444. console.log("order_number:" + that.data.order_number);
  445. console.log("shareTime:" + that.data.shareTime);
  446. console.log("order_number_send:" + that.data.order_number_send);
  447. //查询同一分享链接,礼物接收状态【根据订单号和时间做唯一标识,0为未被接收,1为被接收, 2为超时未领取】
  448. wx.request({
  449. url: host + "orderapi/findlinkByOrderNumber",
  450. data: {
  451. order_number: that.data.order_number,
  452. order_timeno: that.data.shareTime
  453. },
  454. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  455. header: {
  456. 'Accept': 'application/json'
  457. },
  458. success: function (res) {
  459. console.log(res.data);
  460. if (res.data == 0) {
  461. that.setData({
  462. wrapShowFlag: 'wrapShow',
  463. })
  464. }
  465. that.setData({
  466. linkFlag: res.data,
  467. footerHidden: false,
  468. loadingFinish: true
  469. })
  470. wx.hideLoading();
  471. }
  472. })
  473. }
  474. if (res == null || res.data == null) {
  475. console.error('网络请求失败');
  476. return;
  477. }
  478. }
  479. });
  480. if (un_id != undefined) {
  481. that.setData({
  482. userInfo: getApp().globalData.userInfo,
  483. })
  484. console.log("un_id::" + un_id);
  485. }
  486. },
  487. onHide:function(){
  488. var that = this;
  489. that.setData({
  490. wrapShowFlag: '',
  491. loadingFinish:false
  492. })
  493. wx.hideLoading();
  494. },
  495. //页面卸载[完全关闭]
  496. onUnload:function () {
  497. var that = this;
  498. that.setData({
  499. wrapShowFlag: '',
  500. loadingFinish: false,
  501. })
  502. wx.hideLoading();
  503. },
  504. selectWuliu: function () {
  505. var that = this;
  506. wx.navigateTo({
  507. url: '/pages/wuliu/wuliu?id=' + that.data.order_number,
  508. success: function (res) { },
  509. fail: function (res) { },
  510. complete: function (res) { },
  511. })
  512. },
  513. wantSent: function (e) {
  514. wx.switchTab({
  515. url: '/pages/index/index'
  516. })
  517. },
  518. previewImg: function (e) {//banner图预览
  519. var that = this;
  520. var currentUrl = e.currentTarget.dataset.src;
  521. wx.previewImage({
  522. current: currentUrl,
  523. urls: that.data.uploadImg.imgList
  524. })
  525. },
  526. getGift: function () {//领取礼物
  527. var that = this;
  528. var host = getApp().globalData.servsers;
  529. var order_number = that.data.order_number;
  530. wx.navigateTo({
  531. url: '/pages/addAddressFriend/addAddressFriend?order_number=' + order_number
  532. })
  533. },
  534. })