// package_first/pages/askDetail/index.js
import {getPoint} from '../../../api/home.js'
Page({

   /**
     * 页面的初始数据
     */
    data: {
        dataSource: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options) {
            const dataSource = JSON.parse(decodeURIComponent(options.data))
            let imgs=dataSource.content.match(/<img.*?src="(.*)".*?\/>/g);
            if(!imgs){
                this.setData({dataSource})
            } else {
                for(let img of imgs){
                    let temp
                    temp=img.replace(/(width=".*?"|height=".*?")/g,"");
                    if(/style="(.*?)"/.test(temp)){
                        temp=temp.replace(RegExp.$1,RegExp.$1+"width:100% !important;height:auto;display:block")
                    }else{
                        temp=temp.replace(/<img/,"<img style='width:100% !important;height:auto;display:block'")
                    }
                    dataSource.content=dataSource.content.replace(img,temp)
                }
                this.setData({dataSource})
            }
        }
    },
    // 领取积分
    getPoints() {
        let params = {}
        params.openid = wx.getStorageSync('openid')
        params.knowId = this.data.dataSource.id
        getPoint(params).then(res=> {
            if(res.code === 0) {
                wx.navigateBack({
                  delta: 0,
                })
            }
        })
    }
})