util.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. function getdomain() {
  2. var app = getApp();
  3. var new_domain = app.siteInfo.uniacid + '_' + app.siteInfo.siteroot;
  4. var api = new_domain;
  5. return api;
  6. }
  7. function api() {
  8. var api = 'https://mall.shiziyu888.com/dan/';
  9. return api;
  10. }
  11. function check_login() {
  12. var token = wx.getStorageSync('token');
  13. var member_id = wx.getStorageSync('member_id');
  14. if (token && member_id != undefined && member_id.length > 0) {
  15. return true;
  16. } else {
  17. return false;
  18. }
  19. }
  20. /**
  21. * s_link: 回调链接
  22. * type:跳转方式 0 redirectTo, 1 switchTab
  23. */
  24. function login(s_link, type = 0) {
  25. var app = getApp();
  26. var share_id = wx.getStorageSync('share_id');
  27. if (share_id == undefined) {
  28. share_id = '0';
  29. }
  30. wx.login({
  31. success: function(res) {
  32. if (res.code) {
  33. console.log(res.code);
  34. app.util.request({
  35. 'url': 'entry/wxapp/user',
  36. 'data': {
  37. controller: 'user.applogin',
  38. 'code': res.code
  39. },
  40. dataType: 'json',
  41. success: function(res) {
  42. console.log(res);
  43. wx.setStorage({
  44. key: "token",
  45. data: res.data.token
  46. })
  47. wx.getUserInfo({
  48. success: function(msg) {
  49. var userInfo = msg.userInfo
  50. wx.setStorage({
  51. key: "userInfo",
  52. data: userInfo
  53. })
  54. console.log(msg.userInfo);
  55. app.util.request({
  56. 'url': 'entry/wxapp/user',
  57. 'data': {
  58. controller: 'user.applogin_do',
  59. 'token': res.data.token,
  60. share_id: share_id,
  61. nickName: msg.userInfo.nickName,
  62. avatarUrl: msg.userInfo.avatarUrl,
  63. encrypteddata: msg.encryptedData,
  64. iv: msg.iv
  65. },
  66. method: 'post',
  67. dataType: 'json',
  68. success: function(res) {
  69. wx.setStorage({
  70. key: "member_id",
  71. data: res.data.member_id
  72. })
  73. wx.showToast({
  74. title: '资料已更新',
  75. icon: 'success',
  76. duration: 2000,
  77. success: function() {
  78. //s_link
  79. if (s_link && s_link.length > 0){
  80. if (type == 1) {
  81. wx.switchTab({
  82. url: s_link,
  83. })
  84. } else {
  85. wx.redirectTo({
  86. url: s_link
  87. })
  88. }
  89. }
  90. }
  91. })
  92. }
  93. })
  94. },
  95. fail: function(msg) {
  96. // console.log(msg);
  97. }
  98. })
  99. }
  100. });
  101. } else {
  102. //console.log('获取用户登录态失败!' + res.errMsg)
  103. }
  104. }
  105. })
  106. }
  107. function login_prosime() {
  108. var app = getApp();
  109. var share_id = wx.getStorageSync('share_id');
  110. if (share_id == undefined) {
  111. share_id = '0';
  112. }
  113. return new Promise(function (resolve, reject) {
  114. wx.login({
  115. success: function (res) {
  116. if (res.code) {
  117. console.log(res.code);
  118. app.util.request({
  119. 'url': 'entry/wxapp/user',
  120. 'data': {
  121. controller: 'user.applogin',
  122. 'code': res.code
  123. },
  124. dataType: 'json',
  125. success: function (res) {
  126. console.log(res);
  127. wx.setStorage({
  128. key: "token",
  129. data: res.data.token
  130. })
  131. wx.getUserInfo({
  132. success: function (msg) {
  133. var userInfo = msg.userInfo
  134. wx.setStorage({
  135. key: "userInfo",
  136. data: userInfo
  137. })
  138. console.log(msg.userInfo);
  139. app.util.request({
  140. 'url': 'entry/wxapp/user',
  141. 'data': {
  142. controller: 'user.applogin_do',
  143. 'token': res.data.token,
  144. share_id: share_id,
  145. nickName: msg.userInfo.nickName,
  146. avatarUrl: msg.userInfo.avatarUrl,
  147. encrypteddata: msg.encryptedData,
  148. iv: msg.iv
  149. },
  150. method: 'post',
  151. dataType: 'json',
  152. success: function (res) {
  153. wx.setStorage({
  154. key: "member_id",
  155. data: res.data.member_id
  156. })
  157. getCommunityInfo();
  158. resolve(res);
  159. }
  160. })
  161. },
  162. fail: function (msg) {
  163. reject(msg)
  164. }
  165. })
  166. }
  167. });
  168. } else {
  169. reject(res.errMsg)
  170. }
  171. }
  172. })
  173. })
  174. }
  175. function stringToJson(data) {
  176. return JSON.parse(data);
  177. }
  178. function jsonToString(data) {
  179. return JSON.stringify(data);
  180. }
  181. function imageUtil(e) {
  182. var imageSize = {};
  183. var originalWidth = e.detail.width; //图片原始宽
  184. var originalHeight = e.detail.height; //图片原始高
  185. var originalScale = originalHeight / originalWidth; //图片高宽比
  186. //获取屏幕宽高
  187. wx.getSystemInfo({
  188. success: function(res) {
  189. var windowWidth = res.windowWidth;
  190. var windowHeight = res.windowHeight;
  191. var windowscale = windowHeight / windowWidth; //屏幕高宽比
  192. //console.log('windowWidth: ' + windowWidth)
  193. //console.log('windowHeight: ' + windowHeight)
  194. if (originalScale < windowscale) { //图片高宽比小于屏幕高宽比
  195. //图片缩放后的宽为屏幕宽
  196. imageSize.imageWidth = windowWidth;
  197. imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth;
  198. } else { //图片高宽比大于屏幕高宽比
  199. //图片缩放后的高为屏幕高
  200. imageSize.imageHeight = windowHeight;
  201. imageSize.imageWidth = (windowHeight * originalWidth) / originalHeight;
  202. }
  203. }
  204. })
  205. //console.log('缩放后的宽: ' + imageSize.imageWidth)
  206. //console.log('缩放后的高: ' + imageSize.imageHeight)
  207. return imageSize;
  208. }
  209. const formatTime = date => {
  210. const year = date.getFullYear()
  211. const month = date.getMonth() + 1
  212. const day = date.getDate()
  213. const hour = date.getHours()
  214. const minute = date.getMinutes()
  215. const second = date.getSeconds()
  216. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  217. }
  218. const formatNumber = n => {
  219. n = n.toString()
  220. return n[1] ? n : '0' + n
  221. }
  222. //获取社区存本地
  223. const getCommunityInfo = function (param={}) {
  224. let community = wx.getStorageSync('community');
  225. let app = getApp();
  226. let that = this;
  227. var token = wx.getStorageSync('token');
  228. return new Promise(function (resolve, reject) {
  229. if (!community){
  230. app.util.request({
  231. url: 'entry/wxapp/index',
  232. data: {
  233. controller: 'index.load_history_community',
  234. token: token
  235. },
  236. dataType: 'json',
  237. success: function (res) {
  238. if (res.data.code == 0) {
  239. let history_communities = res.data.list;
  240. if (Object.keys(history_communities).length > 0 || history_communities.communityId != 0){
  241. wx.setStorageSync('community', history_communities);
  242. app.globalData.community = history_communities;
  243. resolve(history_communities);
  244. } else {
  245. resolve('');
  246. }
  247. }else{
  248. console.log(param)
  249. if (check_login() && param.communityId === void 0){
  250. wx.redirectTo({
  251. url: '/lionfish_comshop/pages/position/community',
  252. })
  253. resolve('');
  254. } else {
  255. resolve(param);
  256. }
  257. }
  258. }
  259. })
  260. } else {
  261. resolve('')
  262. }
  263. })
  264. }
  265. module.exports = {
  266. formatTime: formatTime,
  267. login: login,
  268. check_login: check_login,
  269. api: api,
  270. getdomain: getdomain,
  271. imageUtil: imageUtil,
  272. jsonToString: jsonToString,
  273. stringToJson: stringToJson,
  274. login_prosime,
  275. getCommunityInfo
  276. }