轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

微信小程序 MD5js使用方法,请求接口轮播图

发布者: kwan1122 | 发布时间: 2018-5-19 20:24| 查看数: 3911| 评论数: 1|帖子模式

作者:woshihaiyong168,来自原文地址

一:MD5 方法js

更多md5相关内容,可以查看此帖:MD5加密中文的结果不一致 
生成的文件可以放在 utils文件中哦!!!

  1. /*
  2. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  3. * Digest Algorithm, as defined in RFC 1321.
  4. * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002.
  5. * Code also contributed by Greg Holt
  6. * See for details.
  7. */
  8. /*
  9. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  10. * to work around bugs in some JS interpreters.
  11. */
  12. function safe_add(x, y)
  13. {
  14. var lsw = (x & 0xFFFF) + (y & 0xFFFF)
  15. var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
  16. return (msw << 16) | (lsw & 0xFFFF)
  17. }
  18. /*
  19. * Bitwise rotate a 32-bit number to the left.
  20. */
  21. function rol(num, cnt)
  22. {
  23. return (num << cnt) | (num >>> (32 - cnt))
  24. }
  25. /*
  26. * These functions implement the four basic operations the algorithm uses.
  27. */
  28. function cmn(q, a, b, x, s, t)
  29. {
  30. return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)
  31. }
  32. function ff(a, b, c, d, x, s, t)
  33. {
  34. return cmn((b & c) | ((~b) & d), a, b, x, s, t)
  35. }
  36. function gg(a, b, c, d, x, s, t)
  37. {
  38. return cmn((b & d) | (c & (~d)), a, b, x, s, t)
  39. }
  40. function hh(a, b, c, d, x, s, t)
  41. {
  42. return cmn(b ^ c ^ d, a, b, x, s, t)
  43. }
  44. function ii(a, b, c, d, x, s, t)
  45. {
  46. return cmn(c ^ (b | (~d)), a, b, x, s, t)
  47. }
  48. /*
  49. * Calculate the MD5 of an array of little-endian words, producing an array
  50. * of little-endian words.
  51. */
  52. function coreMD5(x)
  53. {
  54. var a = 1732584193
  55. var b = -271733879
  56. var c = -1732584194
  57. var d = 271733878
  58. for(var i = 0; i < x.length; i += 16)
  59. {
  60. var olda = a
  61. var oldb = b
  62. var oldc = c
  63. var oldd = d
  64. a = ff(a, b, c, d, x[i+ 0], 7 , -680876936)
  65. d = ff(d, a, b, c, x[i+ 1], 12, -389564586)
  66. c = ff(c, d, a, b, x[i+ 2], 17, 606105819)
  67. b = ff(b, c, d, a, x[i+ 3], 22, -1044525330)
  68. a = ff(a, b, c, d, x[i+ 4], 7 , -176418897)
  69. d = ff(d, a, b, c, x[i+ 5], 12, 1200080426)
  70. c = ff(c, d, a, b, x[i+ 6], 17, -1473231341)
  71. b = ff(b, c, d, a, x[i+ 7], 22, -45705983)
  72. a = ff(a, b, c, d, x[i+ 8], 7 , 1770035416)
  73. d = ff(d, a, b, c, x[i+ 9], 12, -1958414417)
  74. c = ff(c, d, a, b, x[i+10], 17, -42063)
  75. b = ff(b, c, d, a, x[i+11], 22, -1990404162)
  76. a = ff(a, b, c, d, x[i+12], 7 , 1804603682)
  77. d = ff(d, a, b, c, x[i+13], 12, -40341101)
  78. c = ff(c, d, a, b, x[i+14], 17, -1502002290)
  79. b = ff(b, c, d, a, x[i+15], 22, 1236535329)
  80. a = gg(a, b, c, d, x[i+ 1], 5 , -165796510)
  81. d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632)
  82. c = gg(c, d, a, b, x[i+11], 14, 643717713)
  83. b = gg(b, c, d, a, x[i+ 0], 20, -373897302)
  84. a = gg(a, b, c, d, x[i+ 5], 5 , -701558691)
  85. d = gg(d, a, b, c, x[i+10], 9 , 38016083)
  86. c = gg(c, d, a, b, x[i+15], 14, -660478335)
  87. b = gg(b, c, d, a, x[i+ 4], 20, -405537848)
  88. a = gg(a, b, c, d, x[i+ 9], 5 , 568446438)
  89. d = gg(d, a, b, c, x[i+14], 9 , -1019803690)
  90. c = gg(c, d, a, b, x[i+ 3], 14, -187363961)
  91. b = gg(b, c, d, a, x[i+ 8], 20, 1163531501)
  92. a = gg(a, b, c, d, x[i+13], 5 , -1444681467)
  93. d = gg(d, a, b, c, x[i+ 2], 9 , -51403784)
  94. c = gg(c, d, a, b, x[i+ 7], 14, 1735328473)
  95. b = gg(b, c, d, a, x[i+12], 20, -1926607734)
  96. a = hh(a, b, c, d, x[i+ 5], 4 , -378558)
  97. d = hh(d, a, b, c, x[i+ 8], 11, -2022574463)
  98. c = hh(c, d, a, b, x[i+11], 16, 1839030562)
  99. b = hh(b, c, d, a, x[i+14], 23, -35309556)
  100. a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060)
  101. d = hh(d, a, b, c, x[i+ 4], 11, 1272893353)
  102. c = hh(c, d, a, b, x[i+ 7], 16, -155497632)
  103. b = hh(b, c, d, a, x[i+10], 23, -1094730640)
  104. a = hh(a, b, c, d, x[i+13], 4 , 681279174)
  105. d = hh(d, a, b, c, x[i+ 0], 11, -358537222)
  106. c = hh(c, d, a, b, x[i+ 3], 16, -722521979)
  107. b = hh(b, c, d, a, x[i+ 6], 23, 76029189)
  108. a = hh(a, b, c, d, x[i+ 9], 4 , -640364487)
  109. d = hh(d, a, b, c, x[i+12], 11, -421815835)
  110. c = hh(c, d, a, b, x[i+15], 16, 530742520)
  111. b = hh(b, c, d, a, x[i+ 2], 23, -995338651)
  112. a = ii(a, b, c, d, x[i+ 0], 6 , -198630844)
  113. d = ii(d, a, b, c, x[i+ 7], 10, 1126891415)
  114. c = ii(c, d, a, b, x[i+14], 15, -1416354905)
  115. b = ii(b, c, d, a, x[i+ 5], 21, -57434055)
  116. a = ii(a, b, c, d, x[i+12], 6 , 1700485571)
  117. d = ii(d, a, b, c, x[i+ 3], 10, -1894986606)
  118. c = ii(c, d, a, b, x[i+10], 15, -1051523)
  119. b = ii(b, c, d, a, x[i+ 1], 21, -2054922799)
  120. a = ii(a, b, c, d, x[i+ 8], 6 , 1873313359)
  121. d = ii(d, a, b, c, x[i+15], 10, -30611744)
  122. c = ii(c, d, a, b, x[i+ 6], 15, -1560198380)
  123. b = ii(b, c, d, a, x[i+13], 21, 1309151649)
  124. a = ii(a, b, c, d, x[i+ 4], 6 , -145523070)
  125. d = ii(d, a, b, c, x[i+11], 10, -1120210379)
  126. c = ii(c, d, a, b, x[i+ 2], 15, 718787259)
  127. b = ii(b, c, d, a, x[i+ 9], 21, -343485551)
  128. a = safe_add(a, olda)
  129. b = safe_add(b, oldb)
  130. c = safe_add(c, oldc)
  131. d = safe_add(d, oldd)
  132. }
  133. return [a, b, c, d]
  134. }
  135. /*
  136. * Convert an array of little-endian words to a hex string.
  137. */
  138. function binl2hex(binarray)
  139. {
  140. var hex_tab = "0123456789abcdef"
  141. var str = ""
  142. for(var i = 0; i < binarray.length * 4; i++)
  143. {
  144. str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
  145. hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF)
  146. }
  147. return str
  148. }
  149. /*
  150. * Convert an array of little-endian words to a base64 encoded string.
  151. */
  152. function binl2b64(binarray)
  153. {
  154. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  155. var str = ""
  156. for(var i = 0; i < binarray.length * 32; i += 6)
  157. {
  158. str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) |
  159. ((binarray[i>>5+1] >> (32-i%32)) & 0x3F))
  160. }
  161. return str
  162. }
  163. /*
  164. * Convert an 8-bit character string to a sequence of 16-word blocks, stored
  165. * as an array, and append appropriate padding for MD4/5 calculation.
  166. * If any of the characters are >255, the high byte is silently ignored.
  167. */
  168. function str2binl(str)
  169. {
  170. var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks
  171. var blks = new Array(nblk * 16)
  172. for(var i = 0; i < nblk * 16; i++) blks[i] = 0
  173. for(var i = 0; i < str.length; i++)
  174. blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8)
  175. blks[i>>2] |= 0x80 << ((i%4) * 8)
  176. blks[nblk*16-2] = str.length * 8
  177. return blks
  178. }
  179. /*
  180. * Convert a wide-character string to a sequence of 16-word blocks, stored as
  181. * an array, and append appropriate padding for MD4/5 calculation.
  182. */
  183. function strw2binl(str)
  184. {
  185. var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks
  186. var blks = new Array(nblk * 16)
  187. for(var i = 0; i < nblk * 16; i++) blks[i] = 0
  188. for(var i = 0; i < str.length; i++)
  189. blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16)
  190. blks[i>>1] |= 0x80 << ((i%2) * 16)
  191. blks[nblk*16-2] = str.length * 16
  192. return blks
  193. }
  194. /*
  195. * External interface
  196. */
  197. function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) }
  198. function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }
  199. function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) }
  200. function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }
  201. /* Backward compatibility */
  202. function calcMD5(str) { return binl2hex(coreMD5( str2binl(str))) }
  203. module.exports = {
  204. hexMD5: hexMD5
  205. }

使用方式 :

  1. var utilMd5 = require('../../utils/md5.js');
  2. var password = utilMd5.hexMD5(password);

二:请求接口轮播

最近小程序特别火,笔者也来一探究竟!

这次我们采用了tpshop开源框架与微信小程序接口写了一个简单的轮播图栗子!

效果展示: 

实现思路:

通过小程序post请求调取toshop接口返回json数据,之后利用小程序标签swiper 来实现轮播图效果!!!

代码示例: 
准备工作:

因为我们要使用md5来加密生成与接口的秘钥而js中没有PHPmd5方法,所以我们需要添加一个小程序版的md5.js 文件 
md5.js 本文第一部分即是

因为我们post提交值 但是我们传值必须要转译一下,所以我们在util.js文件中加入以下代码:

  1. function json2Form(json) {
  2. var str = [];
  3. for(var p in json){
  4. str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
  5. }
  6. return str.join("&");
  7. }
  8. module.exports = {
  9. json2Form:json2Form,
  10. }

下面展示代码: 
index.js 请求接口

  1. //index.js
  2. //获取应用实例
  3. // pages/shop/index.js
  4. var utilMd5 = require('../../utils/md5.js');
  5. var util = require('../../utils/util.js');
  6. Page({
  7. data:{
  8. duration: 2000,
  9. indicatorDots: true,
  10. autoplay: true,
  11. interval: 4000,
  12. loading: false,
  13. plain: false
  14. },
  15. onLoad:function(options){
  16. //获去轮播信息
  17. var that = this;
  18. var username = '13800138006';
  19. var password = '123456';
  20. var unique_id = '789789';
  21. var key = 'shop';
  22. var time = Date.now();
  23. var sign = utilMd5.hexMD5(password+unique_id+username+time+key);
  24. console.log(time);
  25. console.log(sign);
  26. // that = this;
  27. wx.request( {
  28. url: "",
  29. header: {
  30. "Content-Type": "application/x-www-form-urlencoded"
  31. },
  32. method: "POST",
  33. data: util.json2Form( {
  34. username: username,
  35. password: password,
  36. unique_id: unique_id,
  37. time : time,
  38. sign : sign
  39. }),
  40. success: function(res) {
  41. that.setData({
  42. bannerImg: res.data.result.ad,
  43. goodsList: res.data.result.goods
  44. })
  45. console.log(res.data)
  46. console.log(res.data.result.ad),
  47. console.log(res.data.result.goods)
  48. }
  49. })
  50. // 页面初始化 options为页面跳转所带来的参数
  51. },
  52. onReady:function(){
  53. // 页面渲染完成
  54. },
  55. onShow:function(){
  56. // 页面显示
  57. },
  58. onHide:function(){
  59. // 页面隐藏
  60. },
  61. onUnload:function(){
  62. // 页面关闭
  63. }
  64. })

index.wxml

  1. <!--index.wxml-->
  2. <!--轮播 -->
  3. <view class="banner" >
  4. <swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" class="banners" interval="{{interval}}" duration="{{duration}}">
  5. <block wx:for="{{bannerImg}}" wx:key="{{banner}}">
  6. <swiper-item class="banner" >
  7. <image src="{{item.ad_code}}" data-id="{{item.ad_link}}" bindtap="bindViewTap" class="banner-image" />
  8. <!--<text class="banner-title">{{item.ad_name}}</text>-->
  9. </swiper-item>
  10. </block>
  11. </swiper>
  12. </view>

index..wxss

  1. .swiper{
  2. width:100%;
  3. height:300rpx
  4. }
  5. swiper image{
  6. width:100%;
  7. height:300rpx
  8. }

之后就完成操作了!!!!!!

最新评论

404NotFound 发表于 2022-6-17 08:49
源代码怎么下载音乐

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表