Odoo + 微信小程序快速搭建商城补充订单评价功能

本文承接前文,针对用户确认收货后提交评价无法查看的问题进行处理。先建立评价模型,关联产品、订单、用户等。接着完善评价相关接口,包括找到并完善保存和展示评价的接口。然后在odoo后台增加评价视图,配置用户分组权限,最后进行了效果展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

承接上一篇的内容。
传送门:https://blog.youkuaiyun.com/qq_37816503/article/details/95122104
当用户确认收货后,订单状态会来到待评价,提交评价后无法看到评价,经过查看后台后发现保存评价的接口是没有完成,所以决定自己动动手玩玩。

建立评价模型

首先创建评价的模型,除了具体评论内容,和好中差评论的选项外,还关联产品,订单,用户等模型。

class Reputation(models.Model):
    _name = 'wxapp.reputation'
    _order = 'write_date DESC'

    remark = fields.Char('评论')
    reputation = fields.Selection(
        [('0', '差评'),
         ('1', '中评'),
         ('2', '好评')],
        default='2',
    )
    product_id = fields.Many2one('product.template', string='关联产品')
    order_id = fields.Many2one('sale.order', string='关联订单')
    wxapp_user_id = fields.Many2one('wxapp.user', string='关联用户')

完善评价相关接口

由于前端功能是完整的,所有只需要根据前端找到相应功能的访问了什么接口就可以去完善这些对应接口啦,前后分离真是个好东西。

1.找到相关接口

首先,根据”提交评论”这个关键字在前端项目中全局搜索,找到这个按钮的提交方法submitReputation,再到submitReputation这个js方法中找到访问了WXAPI.orderReputation这个接口,即”/order/reputation”这个后缀的接口。

然后,就要找到展示评价这个接口了,评价是在商品的详情中,打开商品详情的wxml,可以找到用于展示评价的一些字段,最引人瞩目的就是”wx:for="{{reputation}}”,一定是访问接口返回的reputation,于是顺藤摸瓜摸到对应的js文件中找到”WXAPI.goodsReputation”对应的是”/shop/goods/reputation”。

所以,需要操作的主要就是这两个接口,如果在controllers已经存在的接口则只需要补充,否则是需要再新建一个controller。

ps.人比较笨只能这样慢慢找啦哈哈哈哈

2.完善保存评价接口

只有保存评论这一部分需要补充。
reputations是前端传回来数据。

# 保存评论
reputations = post_json.get('reputations', [])
for reputation in reputations:
    rep = request.env['wxapp.reputation'].sudo()
    rep.create(
        {'remark': reputation['remark'], 'reputation': reputation['reputation'],
         'wxapp_user_id': wechat_user.id, 'order_id': order.id,
         'product_id': request.env['sale.order.line'].sudo().browse(int(reputation['id'])).product_tmpl_id.id})

3.完善评价展示接口

data = list()
rep = request.env['wxapp.reputation'].sudo().search([('product_id', '=', int(goodsId))])
if not rep:
    return self.res_ok(data)

for obj in rep:
    data.append({'goods': {
        'goodReputationRemark': obj.remark,
        'goodReputationStr': defs.ReputationStatus.attrs[int(obj.reputation)],
        'dateReputation': obj.write_date
    },
        'user': {'avatarUrl': obj.wxapp_user_id.avatar_url}})

在odoo后台中增加评价视图

想要在odoo后台查看或者编辑就要在views加入视图。

1.列表

<!-- 列表 -->
<record id="wxapp_reputation_view_tree_1004" model="ir.ui.view">
    <field name="name">wxapp.reputation.view_tree</field>
    <field name="model">wxapp.reputation</field>
    <field name="type">tree</field>
    <field name="priority">999</field>
    <field name="arch" type="xml">
        <tree string="评价管理">
            <field name="product_id"/>
            <field name="order_id"/>
            <field name="wxapp_user_id"/>
            <field name="reputation"/>
            <field name="remark"/>
            <field name="write_date"/>
        </tree>
    </field>
</record>

2.表单

<!-- 表单 -->
<record id="wxapp_reputation_view_form_1005" model="ir.ui.view">
    <field name="name">wxapp.reputation.view_form</field>
    <field name="model">wxapp.reputation</field>
    <field name="type">form</field>
    <field name="priority">999</field>
    <field name="arch" type="xml">
        <form string="评价管理" version="7.0">
            <sheet>
                <group>
                    <field name="product_id"/>
                    <field name="order_id"/>
                    <field name="wxapp_user_id"/>
                    <field name="reputation"/>
                    <field name="remark"/>
                    <field name="write_date"/>
                </group>
            </sheet>
        </form>
    </field>
</record>

3.菜单

<record id="wxapp_reputation_action" model="ir.actions.act_window">
    <field name="name">评价管理</field>
    <field name="res_model">wxapp.reputation</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="target">current</field>
    <field name="help" type="html">
        <p></p>
    </field>
    <field name="view_ids"
           eval="[(5, 0, 0),
                  (0, 0, {'view_mode': 'tree', 'view_id': ref('wxapp_reputation_view_tree_1004')}),
                  (0, 0, {'view_mode': 'form', 'view_id': ref('wxapp_reputation_view_form_1005')}),
            ]"/>
</record>

<!-- 定义菜单 -->
<menuitem action="wxapp_reputation_action" id="wxapp_reputation_menuitem" name="评价管理"
          parent="parent_menu_319_1536754696" sequence="10"/>

4.添加用户分组权限

在security文件夹中的csv文件中可配置。

效果展示

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值