request.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. let promise = new Promise(function(resolve, reject) {
  122. if (e.mp.detail.errMsg === "getPhoneNumber:ok") {
  123. let appId = 'wxa88225ca7712d558'
  124. let sessionKey = userInfo.sessionKey
  125. let encryptedData = e.mp.detail.encryptedData
  126. let iv = e.mp.detail.iv
  127. let data =''
  128. try {
  129. console.log('解密前 encryptedData: ', encryptedData)
  130. console.log('解密前 iv: ', iv)
  131. console.log('解密前 appId: ', appId)
  132. console.log('解密前 sessionKey: ', sessionKey)
  133. data= WXBizDataCrypt.prototype.decryptData(encryptedData, iv, appId, sessionKey)
  134. console.log('解密后 data: ', data)
  135. resolve(data.phoneNumber)
  136. } catch (e) {
  137. uni.showToast({
  138. icon: "none",
  139. title: e,
  140. duration: 3000
  141. });
  142. }
  143. }
  144. })
  145. return promise
  146. }
  147. //同步信息
  148. const syncInfo = (userInfo) => {
  149. let promise = new Promise(function(resolve, reject) {
  150. if (!userInfo.head) userInfo.head ='https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/card_head.png'
  151. if (!userInfo.nickname) {
  152. let code = ""
  153. for (var i = 0; i < 6; i++) {
  154. code += parseInt(Math.random() * 10);
  155. }
  156. userInfo.nickname = 'user' + code
  157. }
  158. baseRequest('commonUserApp', 'edit', {
  159. commonUserInfo: JSON.stringify(userInfo)
  160. }, failres => {
  161. uni.showToast({
  162. icon: "none",
  163. title: failres.errmsg,
  164. duration: 3000
  165. });
  166. reject(err.code)
  167. }).then(res => {
  168. resolve(res.data)
  169. })
  170. })
  171. return promise
  172. }
  173. export default {
  174. baseUrl,
  175. baseRequest,
  176. TokenRequest,
  177. wxlogin,
  178. getPhone,
  179. syncInfo
  180. }