pyip.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // pages/pyip/pyip.js
  2. const COS = require('../../utils/cos.js');
  3. const app = require('../../utils/util.js');
  4. const tools = require('../../utils/tools.js');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. carWin_img_hidden: true, //展示照片的view是否隐藏
  11. carWin_img: '', //存放照片路径的
  12. carWin_img_type: 2, //是图片还是视频
  13. cosPath: "" //腾讯云上传的路径
  14. },
  15. openCamera() {
  16. wx.chooseMedia({
  17. count: 1,
  18. mediaType: ['image', 'video'],
  19. sourceType: ['album', 'camera'],
  20. maxDuration: 30,
  21. camera: 'back',
  22. success: (res) => {
  23. console.log(res, "9999999999999999")
  24. let path = res.tempFiles[0].tempFilePath
  25. let uploadType = path.substring(path.length - 3)
  26. if (uploadType == "jpg" || uploadType == "png") {
  27. this.setData({
  28. carWin_img: path,
  29. carWin_img_hidden: false,
  30. carWin_img_type: 0
  31. })
  32. } else if (uploadType == "mp4") {
  33. this.setData({
  34. carWin_img: path,
  35. carWin_img_hidden: false,
  36. carWin_img_type: 1
  37. })
  38. }
  39. }
  40. })
  41. },
  42. submitImgToTxy() {
  43. if (this.data.carWin_img == "" || this.data.carWin_img == null) {
  44. wx.showModal({
  45. title: '请点击上方选择照片或者视频哦',
  46. showCancel: false
  47. });
  48. return false
  49. }
  50. wx.showLoading({
  51. mask: true,
  52. title: '加载中',
  53. })
  54. // var CosAuth = require('./cos-auth'); // COS 签名方法 https://unpkg.com/cos-js-sdk-v5/demo/common/cos-auth.min.js
  55. var Bucket = 'bucketnj1-1254259530';
  56. var Region = 'ap-nanjing';
  57. var ForcePathStyle = false; // 是否使用后缀式,涉及签名计算和域名白名单配置,后缀式说明看上文
  58. // var uploadFile = function () {
  59. // 请求用到的参数
  60. var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
  61. if (ForcePathStyle) {
  62. // 后缀式请求在签名时域名使用地域域名,而不是存储桶域名,具体说明见本文上述“3.后缀式请求”
  63. prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/';
  64. }
  65. // 对更多字符编码的 url encode 格式
  66. // var camSafeUrlEncode = function (str) {
  67. // return encodeURIComponent(str)
  68. // .replace(/!/g, '%21')
  69. // .replace(/'/g, '%27')
  70. // .replace(/\(/g, '%28')
  71. // .replace(/\)/g, '%29')
  72. // .replace(/\*/g, '%2A');
  73. // };
  74. // 获取临时密钥
  75. var stsCache;
  76. var getCredentials = function (callback) {
  77. if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) {
  78. callback(data.credentials);
  79. return;
  80. }
  81. wx.request({
  82. method: 'POST',
  83. url: app.globalData.publicUrl + '/wx/course/getSts', // 服务端签名,参考上文说的获取临时密钥
  84. // dataType: 'json',
  85. success: function (result) {
  86. var credentials = result.data.credential.credentials;
  87. if (credentials) {
  88. stsCache = result.data.credential
  89. } else {
  90. wx.showModal({
  91. title: '临时密钥获取失败',
  92. content: JSON.stringify(data),
  93. showCancel: false
  94. });
  95. }
  96. callback(stsCache && stsCache.credentials);
  97. },
  98. error: function (err) {
  99. wx.showModal({
  100. title: '临时密钥获取失败',
  101. content: JSON.stringify(err),
  102. showCancel: false
  103. });
  104. }
  105. });
  106. };
  107. // 计算签名
  108. var getAuthorization = function (options, callback) {
  109. getCredentials(function (credentials) {
  110. callback({
  111. XCosSecurityToken: credentials.sessionToken,
  112. Authorization: COS({
  113. SecretId: credentials.tmpSecretId,
  114. SecretKey: credentials.tmpSecretKey,
  115. Method: options.Method,
  116. Pathname: options.Pathname,
  117. })
  118. });
  119. });
  120. };
  121. // 上传文件
  122. // var uploadFile = function (filePath) {
  123. var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名
  124. // var Key = '456abcd.png';
  125. var signPathname = '/'; // PostObject 接口 Key 是放在 Body 传输,所以请求路径和签名路径是 /
  126. if (ForcePathStyle) {
  127. // 后缀式请求在签名时用的路径,要包含存储桶名称,具体说明见本文上述“3.后缀式请求”
  128. signPathname = '/' + Bucket + '/';
  129. }
  130. getAuthorization({
  131. Method: 'POST',
  132. Pathname: signPathname
  133. }, (AuthData) => {
  134. var requestTask = wx.uploadFile({
  135. url: prefix,
  136. name: 'file',
  137. filePath: this.data.carWin_img,
  138. formData: {
  139. 'key': "paiyipai/" + Key,
  140. 'success_action_status': 200,
  141. 'Signature': AuthData.Authorization,
  142. 'x-cos-security-token': AuthData.XCosSecurityToken,
  143. 'Content-Type': '',
  144. },
  145. success: (res) => {
  146. console.log(res.header.Location, "11111111111111")
  147. // var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/');
  148. if (/^2\d\d$/.test('' + res.statusCode)) {
  149. wx.request({
  150. method: 'PUT',
  151. url: app.globalData.publicUrl + '/wx/shoot',
  152. data: {
  153. shootName: this.data.carWin_img,
  154. shootCosName: Key,
  155. sessionKey: this.data.sessionKey,
  156. shootType: this.data.carWin_img_type
  157. },
  158. success: (res) => {
  159. if (res.data.code == 0) {
  160. console.log(res, "我把照片路径传到服务器啦")
  161. wx.showModal({
  162. title: '上传成功',
  163. showCancel: false
  164. });
  165. this.setData({
  166. // cosPath: res.header.Location,
  167. carWin_img: "",
  168. carWin_img_type: 2,
  169. carWin_img_hidden: true
  170. })
  171. this.getArr(this.data.sessionKey)
  172. } else {
  173. wx.showModal({
  174. title: "上传失败啦",
  175. showCancel: false
  176. });
  177. }
  178. }
  179. });
  180. } else {
  181. wx.showModal({
  182. title: '上传失败',
  183. showCancel: false
  184. });
  185. }
  186. },
  187. fail: function () {
  188. wx.hideLoading();
  189. wx.showModal({
  190. title: '上传失败',
  191. showCancel: false
  192. });
  193. }
  194. });
  195. requestTask.onProgressUpdate(function (res) {
  196. console.log('进度:', res);
  197. if (res.progress == 100) {
  198. wx.hideLoading();
  199. }
  200. });
  201. });
  202. },
  203. getArr(sessionKey) {
  204. wx.request({
  205. url: app.globalData.publicUrl + '/wx/shoot',
  206. method: "get",
  207. data: {
  208. sessionKey
  209. },
  210. success: (res) => {
  211. console.log(res.data.imgList, "我是查到的照片视频列表")
  212. console.log(res.data.videoList, "我是查到的照片视频列表")
  213. this.setData({
  214. imgList: res.data.imgList,
  215. videoList: res.data.videoList
  216. })
  217. }
  218. })
  219. },
  220. /**
  221. * 生命周期函数--监听页面加载
  222. */
  223. async onLoad() {
  224. const sessionKey = await tools.checkSessionAndLogin();
  225. this.setData({
  226. sessionKey
  227. })
  228. this.getArr(sessionKey)
  229. // this.getArr(sessionKey);
  230. },
  231. /**
  232. * 生命周期函数--监听页面初次渲染完成
  233. */
  234. onReady: function () {
  235. this.setData({
  236. carWin_img_hidden: true,
  237. carWin_img: ''
  238. });
  239. },
  240. /**
  241. * 生命周期函数--监听页面显示
  242. */
  243. onShow: function () {
  244. },
  245. /**
  246. * 生命周期函数--监听页面隐藏
  247. */
  248. onHide: function () {
  249. },
  250. /**
  251. * 生命周期函数--监听页面卸载
  252. */
  253. onUnload: function () {
  254. },
  255. /**
  256. * 页面相关事件处理函数--监听用户下拉动作
  257. */
  258. onPullDownRefresh: function () {
  259. },
  260. /**
  261. * 页面上拉触底事件的处理函数
  262. */
  263. onReachBottom: function () {
  264. },
  265. /**
  266. * 用户点击右上角分享
  267. */
  268. onShareAppMessage: function () {
  269. }
  270. })