upload.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // pages/upload/upload.js
  2. const app = getApp()
  3. const $api = require('../../utils/api.js').API;
  4. const baseURL = 'https://test.hunanwanzhu.com';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. open:true,
  11. stock_date:'',
  12. stock:[{name:'',fund:''}],
  13. stockList:[],
  14. today_stock_img:[],
  15. value:[-1],
  16. is_markt:0,
  17. today_fund:'',
  18. change:0,
  19. experience:'',
  20. cur:0,
  21. freshen:true,
  22. disable:false,
  23. id:0,
  24. images:[],
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onShow(){
  30. if (!this.data.freshen) {
  31. return;
  32. }
  33. this.setData({
  34. disable: false
  35. })
  36. $api.initUser()
  37. wx.getStorage({
  38. key: 'user',
  39. complete: res => {
  40. this.setData({
  41. role: res.data.role,
  42. })
  43. }
  44. })
  45. $api.getCurrecord().then(res=>{
  46. if (!res.data.data.id){return}
  47. let stock = res.data.data.today_stock
  48. this.setData({
  49. stock: stock.length > 0 ? stock : [{ name: '', fund: '' }],
  50. today_stock_img: res.data.data.today_stock_img,
  51. today_fund: res.data.data.today_fund,
  52. is_markt: res.data.data.is_markt,
  53. change: 1,
  54. experience: res.data.data.experience,
  55. id:res.data.data.id
  56. })
  57. })
  58. },
  59. onLoad: function (options) {
  60. var y=new Date().getFullYear()
  61. var m=Number(new Date().getMonth() + 1)
  62. var d = new Date().getDate()
  63. m=m>9?m:'0'+m;
  64. d=d>9?d:'0'+d
  65. this.setData({
  66. stock_date:y+'-'+m+'-'+d
  67. })
  68. },
  69. /**添加持股 */
  70. add(){
  71. var stock = this.data.stock;
  72. var value=this.data.value;
  73. stock.push({ name: '', fund: '' })
  74. value.push(-1)
  75. this.setData({
  76. stock: stock,
  77. value: value
  78. })
  79. },
  80. del(e){
  81. var i = e.target.dataset.id;
  82. var stock = this.data.stock;
  83. var value = this.data.value;
  84. stock.splice(i,1)
  85. value.splice(i,1)
  86. this.setData({
  87. stock: stock,
  88. value: value
  89. })
  90. },
  91. delToday(e){
  92. this.setData({
  93. today_fund: '',
  94. })
  95. },
  96. bindPickerChange(e){
  97. var i=this.data.cur;
  98. var v = e.target.dataset.name
  99. var stock=this.data.stock;
  100. stock[i].name = v
  101. stock[i].code = e.target.dataset.id
  102. this.setData({
  103. stock: stock
  104. })
  105. setTimeout(()=>{
  106. this.setData({
  107. stockList: []
  108. },50)
  109. })
  110. return;
  111. },
  112. stockSearch(e){
  113. if (e.detail.value == ''){
  114. this.setData({
  115. stockList: []
  116. })
  117. return
  118. }
  119. var i = e.target.dataset.id;
  120. var v = e.detail.value
  121. var stock = this.data.stock;
  122. stock[i].name = v;
  123. this.setData({
  124. stock: stock
  125. })
  126. $api.searchStock({ name: e.detail.value}).then(res=>{
  127. this.setData({
  128. stockList:res.data.data,
  129. cur:e.target.dataset.id
  130. })
  131. })
  132. },
  133. blur(){
  134. // console.log(233)
  135. this.setData({
  136. stockList: []
  137. })
  138. },
  139. nameChange(e){
  140. console.log(e)
  141. var i = e.target.dataset.id;
  142. var v = e.detail.value
  143. var stock = this.data.stock;
  144. stock[i].name = v;
  145. this.setData({
  146. stock: stock
  147. })
  148. },
  149. moneyChange(e){
  150. var i = e.target.dataset.id;
  151. var v = e.detail.value
  152. var stock = this.data.stock;
  153. if (v.split('.').length > 1 && v.split('.')[1].length > 4) {
  154. v = Math.floor(Number(v) * 10000) / 10000
  155. }
  156. stock[i].money = v;
  157. this.setData({
  158. stock: stock
  159. })
  160. },
  161. fundChange(e){
  162. var i = e.target.dataset.id;
  163. var v = e.detail.value
  164. var stock = this.data.stock;
  165. if (v.split('.').length > 1 && v.split('.')[1].length > 4) {
  166. v = Math.floor(Number(v) * 10000) / 10000
  167. }
  168. stock[i].fund = v;
  169. this.setData({
  170. stock: stock
  171. })
  172. },
  173. fundChange1(e) {
  174. var v = e.detail.value
  175. if (v.split('.').length > 1 && v.split('.')[1].length > 4){
  176. v = Math.floor(Number(v)*10000)/10000
  177. }
  178. this.setData({
  179. today_fund: v
  180. })
  181. },
  182. expChange(e){
  183. var v = e.detail.value
  184. this.setData({
  185. experience: v
  186. })
  187. },
  188. switch1Change(e){
  189. this.setData({
  190. is_markt: Number(e.detail.value)
  191. })
  192. },
  193. // oss验签上传图片
  194. upload() {
  195. const that = this
  196. var today_stock_img = that.data.today_stock_img;
  197. wx.chooseMedia({
  198. mediaType: ['image'], // 只能上传图片 还有个属性是可以上传视频
  199. success(res) {
  200. wx.showLoading({
  201. title: '上传中',
  202. mask: true
  203. })
  204. let imgs = []
  205. let valueArr = []
  206. // 需要oss 验签 此处是调取验签的接口
  207. $api.getOssSign(res).then((r) => {
  208. wx.hideLoading()
  209. const arr = res.tempFiles
  210. // arr 是为了获取上传图片反的数组
  211. arr.forEach((e) => {
  212. return imgs.push(e.tempFilePath)
  213. })
  214. for (var i = 0; i < imgs.length; i++) {
  215. let only = imgs[i].split('.')
  216. valueArr.push(
  217. only[only.length - 1]
  218. )
  219. let filename = parseInt(Math.random() * 1000000 + new Date().getTime()) + '.' + valueArr[valueArr.length - 1]
  220. let httpData = {
  221. Key: r.data.dir + filename,
  222. OSSAccessKeyId: r.data.AccessKeyID,
  223. Policy: r.data.policy_encode,
  224. Signature: r.data.signature,
  225. }
  226. wx.uploadFile({
  227. url: baseURL+'/api/wx/uploadfile',
  228. filePath: imgs[i],
  229. formData: httpData,
  230. fileType: 'image',
  231. name: 'file',
  232. timeout: 15000,
  233. header: {
  234. 'Content-Type': 'application/x-www-form-urlencoded',
  235. },
  236. success: (res) => {
  237. if (res.statusCode == 200) {
  238. var data=JSON.parse(res.data)
  239. today_stock_img.push(data.data.url)
  240. that.setData({
  241. today_stock_img: today_stock_img
  242. })
  243. that.loadOcr(imgs[0],data.data.url)
  244. } else {
  245. // 返回错误信息
  246. console.log('error')
  247. }
  248. },
  249. fail: function (res) {
  250. console.log(res);
  251. }
  252. })
  253. }
  254. })
  255. }
  256. })
  257. },
  258. loadOcr(path,url){
  259. var _this = this
  260. console.log(path,url)
  261. wx.getImageInfo({
  262. src:path,
  263. success:function(ires){
  264. _this.loadImageOcr(url,ires.width < ires.height ? 'phone':'pc')
  265. }
  266. })
  267. },
  268. loadImageOcr(img_url,source){
  269. var _this = this
  270. var param = {
  271. img_url:img_url.split('?')[0],
  272. source:source,
  273. }
  274. console.log('img_url',param['img_url'])
  275. console.log('source',param['source'])
  276. $api.imageOcr(param).then(res => {
  277. console.log('ocr',res)
  278. if(res.data.code == 0){
  279. _this.setData({
  280. today_fund:res.data.data.today,
  281. stock:res.data.data.list
  282. })
  283. }
  284. })
  285. },
  286. startOcr(e){
  287. var index = e.target.dataset.index;
  288. var img_url = this.data.today_stock_img[index]
  289. console.log('startocr',img_url)
  290. this.loadImageOcr(img_url,this.data.images[index].width < this.data.images[index].height ? 'phone':'pc')
  291. },
  292. imageLoad: function(e) {
  293. var width=e.detail.width; //获取图片真实宽度
  294. var height=e.detail.height;
  295. var image=this.data.images;
  296. //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
  297. image[e.target.dataset.index]={
  298. width:width,
  299. height:height,
  300. }
  301. this.setData({
  302. images:image
  303. })
  304. },
  305. del1(e) {
  306. var i = e.target.dataset.id;
  307. var today_stock_img = this.data.today_stock_img;
  308. today_stock_img.splice(i, 1)
  309. this.setData({
  310. today_stock_img: today_stock_img
  311. })
  312. },
  313. prview(e){
  314. var src = e.target.dataset.src;
  315. wx.previewImage({
  316. current: src, // 当前显示图片的http链接
  317. urls: [src] // 需要预览的图片http链接列表
  318. })
  319. },
  320. post(){
  321. var stocks = []
  322. var slist = this.data.stock
  323. for(let i=0;i<slist.length;i++){
  324. if(slist[i].name.length <= 0){
  325. continue;
  326. }
  327. if(slist[i].fund > 5000){
  328. wx.showToast({
  329. icon: 'none',
  330. title: '持仓金额单位为万'
  331. })
  332. return;
  333. }
  334. stocks.push(slist[i])
  335. }
  336. var parm = {
  337. stock_date: this.data.stock_date,
  338. today_stock: stocks,
  339. is_markt: this.data.is_markt,
  340. today_stock_img: this.data.today_stock_img,
  341. today_fund: Number(this.data.today_fund),
  342. id:this.data.id,
  343. experience: this.data.experience
  344. }
  345. if (!parm.today_fund) {
  346. wx.showToast({
  347. icon: 'none',
  348. title: '请输入今日净资产'
  349. })
  350. return
  351. }
  352. if (parm.today_fund >= 10000) {
  353. wx.showToast({
  354. icon: 'none',
  355. title: '今日净资产单位为万'
  356. })
  357. return
  358. }
  359. // if (!parm.is_markt) {
  360. // // for (let i = 0; i < parm.today_stock.length;i++){
  361. // // if (!parm.today_stock[i].name || !parm.today_stock[i].fund) {
  362. // // wx.showToast({
  363. // // icon: 'none',
  364. // // title: '请选择股票并输入资金额'
  365. // // })
  366. // // return;
  367. // // break;
  368. // // }
  369. // // }
  370. // } else {
  371. // parm.today_stock = []
  372. // }
  373. console.log(parm)
  374. if (parm.today_stock_img.length <= 0) {
  375. wx.showToast({
  376. icon: 'none',
  377. title: '请上传截图'
  378. })
  379. return
  380. }
  381. wx.showLoading({
  382. title: '正在提交',
  383. })
  384. this.setData({
  385. disable: true
  386. })
  387. $api.updateRecord(parm).then(res => {
  388. wx.hideLoading()
  389. wx.showToast({
  390. title: '提交成功',
  391. })
  392. this.setData({
  393. freshen: true,
  394. disable: false
  395. })
  396. setTimeout(() => {
  397. this.setData({
  398. stock: [{ label: '', name: '', code: '', fund: '' }],
  399. stockList: [],
  400. today_stock_img: [],
  401. value: [-1],
  402. is_markt: 0,
  403. today_fund: ''
  404. })
  405. wx.switchTab({
  406. url: '../user/user',
  407. })
  408. }, 200)
  409. })
  410. .catch(
  411. this.setData({
  412. disable:false
  413. })
  414. )
  415. },
  416. /**
  417. * 生命周期函数--监听页面初次渲染完成
  418. */
  419. onReady: function () {
  420. },
  421. /**
  422. * 生命周期函数--监听页面隐藏
  423. */
  424. onHide: function () {
  425. },
  426. /**
  427. * 生命周期函数--监听页面卸载
  428. */
  429. onUnload: function () {
  430. },
  431. /**
  432. * 页面相关事件处理函数--监听用户下拉动作
  433. */
  434. onPullDownRefresh: function () {
  435. },
  436. /**
  437. * 页面上拉触底事件的处理函数
  438. */
  439. onReachBottom: function () {
  440. },
  441. /**
  442. * 用户点击右上角分享
  443. */
  444. onShareAppMessage: function () {
  445. }
  446. })