request.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import * as config from '@/config'
  2. import WXBizDataCrypt from './WXBizDataCrypt.js'
  3. let baseUrl = config.def().baseUrlNew
  4. // 定义基础请求路径(后端服务器地址)
  5. const baseRequest = (_gp, _mt, data = {}, failCallback) => {
  6. //异步请求数据
  7. return new Promise(resolve => {
  8. let baseUrl = config.def().baseUrlNew
  9. uni.request({
  10. url: baseUrl + '/m.api',
  11. data: {
  12. ...data,
  13. _gp,
  14. _mt
  15. },
  16. method: 'POST',
  17. header: {
  18. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  19. // 'ACCESSTOKEN': accessToken
  20. },
  21. success: (res) => {
  22. if (res.statusCode === 200) {
  23. if (res.data.errno === 200) {
  24. resolve(res.data);
  25. } else {
  26. if (failCallback) {
  27. failCallback(res.data)
  28. } else {
  29. uni.showToast({
  30. title: res.data.errmsg,
  31. icon: 'none'
  32. })
  33. }
  34. }
  35. }
  36. }
  37. })
  38. })
  39. }
  40. //带Token请求
  41. const TokenRequest = (method, url, data, header) => {
  42. var contentheader = 'application/json'
  43. if (header) {
  44. contentheader = header
  45. }
  46. let ac_token = "";
  47. uni.getStorage({
  48. key: 'userInfo',
  49. success: function(res) {
  50. ac_token = res.data.accessToken
  51. }
  52. });
  53. //此token是登录成功后后台返回保存在storage中的
  54. let DefaultOpts = {
  55. url: baseUrl + url,
  56. data: data,
  57. method: method,
  58. header: {
  59. 'content-type': contentheader,
  60. 'Token': ac_token,
  61. }
  62. }
  63. let promise = new Promise(function(resolve, reject) {
  64. uni.request(DefaultOpts).then(
  65. (res) => {
  66. console.log(JSON.stringify(res[1].data))
  67. if (res[1].data.code == '200' || res[1].data.code == 200) {
  68. // 后端返回的状态码100为成功状态,成功则返回请求结果,在app调试时可以通过console.log(JSON.stringify(res[1].data))来查看返回值(以项目实际情况为准)
  69. resolve(res[1].data)
  70. }
  71. if (res[1].data.code == '105' || res[1].data.code == 105) {
  72. // 后端返回状态码为105则为未登录状态(以项目实际情况为准)
  73. uni.showToast({
  74. icon: 'none',
  75. title: '尚未登录',
  76. duration: 2000
  77. });
  78. // 尚未登录的逻辑处理
  79. return false
  80. }
  81. }
  82. ).catch(
  83. (response) => {
  84. reject(response)
  85. }
  86. )
  87. })
  88. return promise
  89. }
  90. //微信登录
  91. const wxlogin = () => {
  92. let promise = new Promise(function(resolve, reject) {
  93. uni.login({
  94. "provider": "weixin",
  95. success: function(wxres) {
  96. console.log("wxlogin",wxres)
  97. baseRequest('commonUserApp', 'commonUserLogin', {
  98. loginType: 1,
  99. raw: JSON.stringify(wxres)
  100. }, failres => {
  101. uni.showToast({
  102. icon: "none",
  103. title: failres.errmsg,
  104. duration: 3000
  105. });
  106. uni.hideLoading()
  107. }).then(res => {
  108. console.log(res.data)
  109. resolve(res.data)
  110. })
  111. },
  112. fail: function(err) {
  113. reject(err.code)
  114. }
  115. })
  116. })
  117. return promise
  118. }
  119. //获取手机号
  120. const getPhone = (e, userInfo) => {
  121. console.log(e,userInfo)
  122. let promise = new Promise(function(resolve, reject) {
  123. if (e.mp.detail.errMsg === "getPhoneNumber:ok") {
  124. let appId = 'wxa88225ca7712d558'
  125. let sessionKey = userInfo.sessionKey
  126. let encryptedData = e.mp.detail.encryptedData
  127. let iv = e.mp.detail.iv
  128. let data =''
  129. try {
  130. console.log('解密前 encryptedData: ', encryptedData)
  131. console.log('解密前 iv: ', iv)
  132. console.log('解密前 appId: ', appId)
  133. console.log('解密前 sessionKey: ', sessionKey)
  134. data= WXBizDataCrypt.prototype.decryptData(encryptedData, iv, appId, sessionKey)
  135. console.log('解密后 data: ', data)
  136. resolve(data.phoneNumber)
  137. } catch (e) {
  138. uni.showToast({
  139. icon: "none",
  140. title: e,
  141. duration: 3000
  142. });
  143. }
  144. }
  145. })
  146. return promise
  147. }
  148. //同步信息
  149. const syncInfo = (userInfo) => {
  150. let promise = new Promise(function(resolve, reject) {
  151. if (!userInfo.head) userInfo.head ='https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/card_head.png'
  152. if (!userInfo.nickname) {
  153. let code = ""
  154. for (var i = 0; i < 6; i++) {
  155. code += parseInt(Math.random() * 10);
  156. }
  157. userInfo.nickname = 'user' + code
  158. }
  159. baseRequest('commonUserApp', 'edit', {
  160. commonUserInfo: JSON.stringify(userInfo)
  161. }, failres => {
  162. uni.showToast({
  163. icon: "none",
  164. title: failres.errmsg,
  165. duration: 3000
  166. });
  167. reject(err.code)
  168. }).then(res => {
  169. resolve(res.data)
  170. })
  171. })
  172. return promise
  173. }
  174. export default {
  175. baseUrl,
  176. baseRequest,
  177. TokenRequest,
  178. wxlogin,
  179. getPhone,
  180. syncInfo
  181. }