login.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // pages/user/login/login.js
  2. const app = getApp()
  3. const api = require('../../../request/api');
  4. const http = require('../../../request/http');
  5. const utils = require('../../../utils/util.js');
  6. const storeKeys = require('../../../utils/storageKeys.js');
  7. Page({
  8. data: {
  9. selected: false,
  10. code:'',
  11. callback:""
  12. },
  13. onLoad(options) {
  14. console.log(options)
  15. this.data.callback = options.callback || ""
  16. },
  17. logIn(){
  18. if(!this.data.selected){
  19. utils.toast("请先同意原味公明腊肠优选服务协议");
  20. }
  21. },
  22. changSelected(){
  23. let selected = this.data.selected;
  24. this.setData({
  25. selected:!selected
  26. })
  27. },
  28. toPage(e){
  29. let type = e.currentTarget.dataset.type;
  30. if(type==1){//服务协议
  31. wx.navigateTo({
  32. url: "/pages/public/pages/serviceAgreement/serviceAgreement"
  33. })
  34. }
  35. if(type==2){//隐私协议
  36. wx.navigateTo({
  37. url: "/pages/public/pages/privacy/privacy"
  38. })
  39. }
  40. },
  41. // 手机号登录/注册
  42. phoneLogin: utils.throttle(function (e) {
  43. wx.navigateTo({
  44. url: "/pages/tabBar/phoneLogin/phoneLogin"
  45. })
  46. }),
  47. getUserInfo: function() {
  48. http.request({
  49. url: api.URL + '/api/user/info',
  50. token: utils.getStorageSync(storeKeys.TOKEN),
  51. method: 'GET',
  52. noLogin: true,
  53. success: function(res) {console.log(res)
  54. let userInfo = res.data.data.userInfo;
  55. app.globalData.userInfo = userInfo
  56. utils.setStorageSync(storeKeys.USERINFO, userInfo);
  57. },
  58. error: function(res) {},
  59. })
  60. },
  61. // 普通用户切换绑定关系
  62. promotion(){
  63. let sharerId = utils.getStorageSync(storeKeys.SHAREID) || utils.getStorageSync(storeKeys.APPLESID) || "";
  64. console.log("sharerId",sharerId)
  65. if(!sharerId){
  66. return;
  67. }
  68. let data = {
  69. sharerId: sharerId,
  70. isScanCode: "1"
  71. }
  72. http.request({
  73. url: api.URL + '/api/promotion/ch_upper',
  74. data: data,
  75. method: 'POST',
  76. token: utils.getStorageSync(storeKeys.TOKEN),
  77. noLogin: true,
  78. noToast:true,
  79. success: function(res) {
  80. console.log("success")
  81. console.log(res)
  82. },
  83. error: function(res) {
  84. console.log("error")
  85. console.log(res)
  86. }
  87. })
  88. },
  89. getLoginCall(params){
  90. let that = this;
  91. console.log(params)
  92. http.request({
  93. url: api.URL + '/api/mp_wx/mobile',
  94. data: params,
  95. method: 'POST',
  96. success: function(res) {
  97. console.log(res)
  98. utils.toast('登录成功');
  99. utils.setStorageSync(storeKeys.TOKEN, res.data.data.token);
  100. that.promotion();
  101. that.getUserInfo()
  102. app.getCartNumber();
  103. setTimeout(() => {
  104. wx.navigateBack({
  105. delta: 1
  106. })
  107. }, 1000)
  108. },
  109. error: function(res) {
  110. console.log("新用户登陆失败")
  111. console.log(res)
  112. }
  113. })
  114. },
  115. getPhoneNumber: function(e) {
  116. let that = this;
  117. let errMsg = e.detail.errMsg;
  118. if (errMsg.indexOf('user cancel') != -1 || errMsg.indexOf('用户未绑定手机,请先在微信客户端进行绑定后重试') != -1) {
  119. console.log('取消');
  120. return;
  121. }
  122. if (errMsg.indexOf('user deny') != -1) {
  123. console.log('拒绝');
  124. return;
  125. }
  126. let pages = getCurrentPages();
  127. let prevPage = pages[pages.length - 2];
  128. let actSourceId = 0;
  129. let pageRoute = prevPage.route;
  130. let actSource = 0
  131. //来源拼团活动
  132. let isGroupShopping = pageRoute.indexOf("activity/pages/groupShopping")!= -1
  133. let isfission = prevPage.options.isfission
  134. // 来源砍价活动
  135. let isBargain = pageRoute.indexOf("activity/pages/bargain")!= -1
  136. // 来源商品详情
  137. let isGoodsDetail = pageRoute.indexOf("cart/pages/goodsDetail")!= -1
  138. if(isGroupShopping){
  139. actSource = 2
  140. if(isfission){
  141. actSource = 4
  142. }
  143. actSourceId = prevPage.data.activity_id || 0;
  144. }
  145. if(isBargain){
  146. actSource = 3
  147. actSourceId = prevPage.data.activity_id || 0;
  148. }
  149. if(isGoodsDetail){
  150. try{
  151. prevPage.countRecord()
  152. }catch(e){}
  153. }
  154. //存在则是来源渠道推广
  155. if(utils.getStorageSync(storeKeys.SOURECID)){
  156. actSourceId = utils.getStorageSync(storeKeys.SOURECID);
  157. actSource = 1
  158. let isCount = false;
  159. if(isGroupShopping){
  160. try{
  161. prevPage.changeCountRecord()
  162. }catch(e){}
  163. isCount = true;
  164. }
  165. if(isBargain){
  166. try{
  167. prevPage.changeCountRecord()
  168. }catch(e){}
  169. isCount = true;
  170. }
  171. !isCount && app.countRecord()
  172. }
  173. let params = {
  174. openid: utils.getStorageSync("openid"),
  175. iv: e.detail.iv,
  176. encryptedData: e.detail.encryptedData,
  177. actSource, // 活动来源 0-其他 1-渠道推广 2拼团活动 3砍价活动 4拼团裂变
  178. actSourceId, // 活动来源详情id 如:渠道推广id
  179. }
  180. console.log(params)
  181. if(that.data.code){
  182. http.request({
  183. url: api.URL + '/api/mp_wx/login',
  184. data: {
  185. // "code":res.code,
  186. "code": that.data.code,
  187. },
  188. method:'POST',
  189. success: function(res) {
  190. utils.setStorageSync(storeKeys.OPENID, res.data.data.openid);
  191. if(res.data.data.token){
  192. utils.toast('登录成功');
  193. utils.setStorageSync(storeKeys.TOKEN, res.data.data.token);
  194. that.getUserInfo()
  195. that.promotion();
  196. app.getCartNumber();
  197. setTimeout(() => {
  198. wx.navigateBack({
  199. delta: 1,
  200. success: () => {
  201. if(!that.data.callback){
  202. return;
  203. }
  204. try{
  205. prevPage.loginCallback()
  206. }catch(e){}
  207. }
  208. })
  209. }, 1000)
  210. }else{
  211. params.openid = res.data.data.openid
  212. that.getLoginCall(params)
  213. }
  214. },
  215. error: function(res) {
  216. }
  217. })
  218. }else{
  219. wx.login({
  220. success: function(res) {
  221. if (res.code) {
  222. http.request({
  223. url: api.URL + '/api/mp_wx/login',
  224. data: {
  225. "code": that.data.code,
  226. },
  227. method:'POST',
  228. success: function(res) {
  229. utils.setStorageSync(storeKeys.OPENID, res.data.data.openid);
  230. if(res.data.data.token){
  231. // clearInterval(that.timer1);
  232. // console.log('清除定时器老用户')
  233. utils.toast('登录成功');
  234. utils.setStorageSync(storeKeys.TOKEN, res.data.data.token);
  235. that.getUserInfo()
  236. that.promotion();
  237. app.getCartNumber();
  238. setTimeout(() => {
  239. wx.navigateBack({
  240. delta: 1,
  241. success: () => {
  242. if(!that.data.callback){
  243. return;
  244. }
  245. try{
  246. prevPage.loginCallback()
  247. }catch(e){}
  248. }
  249. })
  250. }, 1000)
  251. }else{
  252. params.openid = res.data.data.openid
  253. that.getLoginCall(params)
  254. }
  255. },
  256. error: function(res) {
  257. console.log(res)
  258. }
  259. })
  260. }
  261. }
  262. })
  263. }
  264. },
  265. onShow: function () {
  266. let that = this
  267. this.getCode()
  268. },
  269. getCode(){
  270. let that = this
  271. wx.login({
  272. success: function(res) {
  273. that.data.code = res.code
  274. console.log(that.data.code)
  275. }
  276. })
  277. },
  278. })